package com.shopnight.controller.circle;

import com.shopnight.interceptors.JWTInterceptor;
import com.shopnight.result.ApiResult;
import com.shopnight.utils.MyMQTTClient;
import com.shopnight.utils.UserContext;
import com.shopnight.vo.MqttMsgVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/**
 * @Description: 类的描述
 */
@Api(tags = {"MqttController"},description = "mqtt所需接口")
@RestController
@RequestMapping("/api")
public class MqttController {
    /**
     * 客户端
     */
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Autowired
    private MyMQTTClient myMQTTClient;


    private String prefix = "mqtt-topic:";

    /**
     * 创建用户主题
     * @param topicName
     * @return
     */
    @ApiOperation(value = "创建我的mqtt主题",notes = "topicName,传入主题名字")
    @GetMapping("/token/createMyTopic")
    public ApiResult createTopic(String topicName){
        //直接将主题放在缓存中，用的时候从缓存中取出来
        stringRedisTemplate.opsForHash().put(prefix + UserContext.getEndUserId(), topicName ,"0" );
        return ApiResult.ok("" );
    }

    @ApiOperation(value = "创建他的mqtt主题",notes = "userId,topicName,传入用户,主题名字")
    @GetMapping("/token/createTheyTopic")
    public ApiResult createTheyTopic(String userId,String topicName){
        //直接将主题放在缓存中，用的时候从缓存中取出来
        stringRedisTemplate.opsForHash().put(prefix + userId , topicName ,"" );
        return ApiResult.ok("" );
    }

    @ApiOperation(value = "删除我的mqtt主题",notes = "topicName,传入,主题名字")
    @GetMapping("/token/delMyTopic")
    public ApiResult delMyTopic(String topicName){
        //直接将主题放在缓存中，用的时候从缓存中取出来
        stringRedisTemplate.opsForHash().delete(prefix + UserContext.getEndUserId() , topicName);
        return ApiResult.ok("" );
    }

    /**
     * 根据用户获取主题
     * @param
     * @return
     */
    @ApiOperation(value = "获取用户所有mqtt主题",notes = "无需传参")
    @GetMapping("/token/getTopicByUser")
    public ApiResult getTopic(){
        Set<Object> keys = stringRedisTemplate.opsForHash().keys(prefix + UserContext.getEndUserId());
        return ApiResult.ok(keys);
    }

    /**
     * 订阅主题
     */
//    @GetMapping("/subscribeTopic")
//    public ApiResult subscribeTopic(String topic){
//        myMQTTClient.subscribe(topic,1);
//        return ApiResult.ok("订阅"+  topic  +  "主题成功");
//    }
//
//    /**
//     * 取消订阅主题
//     */
//    @PostMapping("/cleanSubscribeTopic")
//    public ApiResult cleanSubscribeTopic(String user){
////        String topicName = redisUtil.get(user).toString();
////        myMQTTClient.cleanTopic(topicName);
//        return ApiResult.ok("取消订阅"+"topicName"+"主题成功");
//    }


    /**
     * 发送消息
     */
    @PostMapping("/sendMsg")
    @ResponseBody
    public  synchronized ApiResult sendMsg(@RequestBody MqttMsgVo mqttMsgVo){
        String result = "给主题：" + mqttMsgVo.getTopicName() + "发送成功";
        //发送消息
//        myMQTTClient.se
        myMQTTClient.publish( mqttMsgVo.getContent(), mqttMsgVo.getTopicName(),  mqttMsgVo.getQos()  );
        return ApiResult.ok(result);
    }


}

